﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MMK.Paket_Tracker.Runtime;
using System.Windows.Controls;
using System.Windows.Input;
using MMK.Paket_Tracker.Runtime.Commands;
using MMK.Paket_Tracker.Runtime.BusinessEntities;
using MMK.Paket_Tracker.GUI.Models;

namespace MMK.Paket_Tracker.GUI.ViewModels
{
    /// <summary>
    /// 
    /// </summary>
    public class GetStatusViewModel : ViewModelBase
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="GetStatusViewModel"/> class.
        /// </summary>
        /// <param name="owner">The owner.</param>
        public GetStatusViewModel(UserControl owner)
        {
            this.owner = owner;
            Data = new GetStatusModel();
            PrepareCommands();
        }

        /// <summary>
        /// Gets or sets the data.
        /// </summary>
        /// <value>The data.</value>
        public GetStatusModel Data { get; set; }

        private void PrepareCommands()
        {
            CommandBinding getStatus = new CommandBinding(PTCommands.GetStatus);
            CommandBinding goBack = new CommandBinding(PTCommands.GoBack);

            getStatus.Executed += getStatus_Executed;
            getStatus.CanExecute += getStatus_CanExecute;

            goBack.Executed += goBack_Executed;
            goBack.CanExecute += goBack_CanExecute;

            owner.CommandBindings.Add(goBack);
            owner.CommandBindings.Add(getStatus);
        }

        void goBack_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }

        void goBack_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            NotifyMainWindow(new MainWindowNotification(((UserControl)sender).Name, null, (RoutedUICommand)e.Command));
        }

        void getStatus_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
//            e.CanExecute = !String.IsNullOrEmpty(Data.PacketID);
            e.CanExecute = (Data.HasError == false);
        }

        void getStatus_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            Data.GetStatus();
        }


    }
}
